﻿#if NET35
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Kernelx.Common.DataAnnotations
{
    /// <summary>
    /// 
    /// </summary>
    public class ValidationContext
    {
        /// <summary>
        /// 
        /// </summary>
        public string DisplayName { get; set; }

        /// <summary>
        /// 
        /// </summary>
        public string MemberName { get; set; }

        /// <summary>
        /// 
        /// </summary>
        public IDictionary<object, object> Items { get; private set; }

        /// <summary>
        /// 
        /// </summary>
        public object ObjectInstance { get; private set; }


        private readonly IServiceProvider _serviceProvider;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="serviceType"></param>
        /// <returns></returns>
        public object GetService(Type serviceType) => _serviceProvider.GetService(serviceType);


        /// <summary>
        /// 
        /// </summary>
        /// <param name="instance"></param>
        public ValidationContext(object instance) => ObjectInstance = instance;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="instance"></param>
        /// <param name="items"></param>
        public ValidationContext(object instance, IDictionary<object, object> items) : this(instance) => Items = items;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="instance"></param>
        /// <param name="serviceProvider"></param>
        /// <param name="items"></param>
        public ValidationContext(object instance, IServiceProvider serviceProvider, IDictionary<object, object> items)
            : this(instance, items) => _serviceProvider = serviceProvider;
    }

    /// <summary>
    /// 
    /// </summary>
    public class ValidationResult
    {
        /// <summary>
        /// 
        /// </summary>
        public string ErrorMessage { get; private set; }

        /// <summary>
        /// 
        /// </summary>
        public IEnumerable<string> MemberNames { get; private set; }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="errorMessage"></param>
        public ValidationResult(string errorMessage) => ErrorMessage = errorMessage;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="errorMessage"></param>
        /// <param name="memberNames"></param>
        public ValidationResult(string errorMessage, IEnumerable<string> memberNames) : this(errorMessage) => MemberNames = memberNames;

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public override string ToString() => ErrorMessage;
    }


    /// <summary>
    /// 
    /// </summary>
    [AttributeUsage(AttributeTargets.Property)]
    public abstract class ValidationAttribute : Attribute
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="value"></param>
        /// <param name="validationContext"></param>
        /// <returns></returns>
        public ValidationResult GetValidationResult(object value, ValidationContext validationContext)
            => IsValid(value, validationContext);


        /// <summary>
        /// 
        /// </summary>
        /// <param name="value"></param>
        /// <param name="validationContext"></param>
        /// <returns></returns>
        protected abstract ValidationResult IsValid(object value, ValidationContext validationContext);
    }
}

#endif